import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86155
 * date: 2024-01-31
 * Time: 19:35
 *
 * @author 数组
 */

public class Test {
    //二维数组练习
    public static void main(String[] args) {
        //不规则数组
        int[][] arr1 = new int[2][];
        int[] arr2 = {1,2,3};
        int[] arr3 = {1,2};
        arr1[0] = arr2;
        arr1[1] = arr3;
        System.out.println(Arrays.deepToString(arr1));
        //数组命名
        int[][] a2 = new int[2][3];
        int[][] a3 = new int[][]{{1, 2, 3}, {4, 5, 6, 7}};
        int[][] a4 = {{1, 2, 3}, {4, 5, 6}};
        //for打印
        for (int i = 0; i < a2.length; i++) {
            for (int j = 0; j < a2[i].length; j++) {
                System.out.print(a2[i][j]+" ");
            }
            System.out.println();
        }
        //利用方法打印
        System.out.println(Arrays.deepToString(a2));
        System.out.println();
        //foreach打印
        for (int[] ar:a2) {
            for (int x:ar) {
                System.out.print(x+" ");
            }
            System.out.println();
        }
    }
    //一维数组练习
    //冒泡排序函数
    public static void bubblSort2(int[] arr){
        for (int i = 0; i < arr.length-1; i++) {
            boolean ret = false;
            for (int j = 0; j < arr.length-1-i; j++) {
                if(arr[j] > arr[j+1]){
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                    ret = true;
                }
            }
            if(ret == false){
                return;
            }
        }
    }
    //冒泡排序函数
    public static void bubblSort(int[] arr){
        for (int i = 0; i < arr.length-1; i++) {
            for (int j = 0; j < arr.length-1-i; j++) {
                if(arr[j] > arr[j+1]){
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
    }
    //冒泡排序
    public static void main15(String[] args) {
        int[] arr = {1,5,6,8,3,4,11,9};
        int[] arr2 = {1,2,3,4,5,6,7};

        bubblSort(arr);
        bubblSort2(arr2);

        System.out.println(Arrays.toString(arr));
        System.out.println(Arrays.toString(arr2));

    }
    //数组逆序
    public static void strary(int[] arr){
        int left = 0;
        int right = arr.length - 1;
        while(left < right){     //判定是否到中间位置
            int temp = arr[left];
            arr[left] = arr[right];  //进行数据交换
            arr[right] = temp;
            left++;
            right--;
        }
    }
    public static void main13(String[] args) {
        int[] arr = {1,2,3,4,5,6,7,8,9,11,10};
        strary(arr);
        System.out.println(Arrays.toString(arr));
    }
    //求数组平均值
    public static double ave(int[] arr){
        int sum = 0;
        for (int x:
             arr) {
            sum += x;
        }
        return sum*1.0/arr.length;
    }
    public static void main12(String[] args) {
        int[] arr = {1,2,3,4,5,6,7,8,9,11,10};
        System.out.println(ave(arr));
    }
    //数组阶段拷贝
    public static void main11(String[] args) {
        int[] arr = {1,2,3,4,5,6,7,8,9,11,10};
        int[] arr1 = {1,2,3,4,5,6,7,8,9,11,10};
        boolean ret = Arrays.equals(arr,arr1);
        System.out.println(ret);
        Arrays.fill(arr,1);
        Arrays.fill(arr,2,9,6);
        System.out.println(Arrays.toString(arr));
    }
    //二分查找
    public static int gufind(int[] arr,int k){
        int left = 0;
        int right = arr.length - 1;
        while(left <= right){
            int mid = (left + right) >>> 2;
            if(k > arr[mid]){
                left = mid +1;
            }else if(k < arr[mid]){
                right = mid -1;
            }else{
                return mid;
            }
        }
        return -1;
    }
    //查找数组中的某一元素
    public static int find(int[] arr,int k){
        int i = 0;
        for (i = 0; i < arr.length; i++) {
            if(k == arr[i]){
                return i;
            }
        }
        return -1;
    }
    public static void main10(String[] args) {
        int[] arr = {1,2,3,4,5,6,7,8,9,11,10};
        Arrays.sort(arr);  //自动排序
        int k = 13;
        int ret = Arrays.binarySearch(arr,11);
        //int ret = gufind(arr,k);
        //int ret = find(arr,k);
        System.out.println(ret);
        //System.out.println(Arrays.toString(arr));
    }
    //求数组平均值
    public static void main9(String[] args) {
        int[] arr = {1,2,3,4,5,6,7,8,9};
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        double ave = sum*1.0/arr.length;
        System.out.println(ave);
    }
    //数组拷贝
    public static void main8(String[] args) {
        int[]arr = new int[]{1,2,3,4,5,15,14,13,11,12,6,7,8,9,10};
       int[] arr2 =  arr.clone();//克隆
        arr2[0] = 99;
        //int[] arr2 = Arrays.copyOfRange(arr,2,5);//左闭右开，比实际少一位
        //System.arraycopy(arr,0,arr2,0,arr.length);//源拷贝
        /*for (int i = 0; i < arr.length; i++) {
            arr2[i] = arr[i];//原始拷贝
        }*/
        //int[] arr2 = Arrays.copyOf(arr,2*arr.length);//空间拷贝

        System.out.println(Arrays.toString(arr));
        System.out.println(Arrays.toString(arr2));

    }
    public static String MYArraystoString(int[] x){
        String ret = "[";
        for (int i = 0; i < x.length; i++) {
            ret += x[i];
            if(i != x.length -1){
                ret += ",";
            }
        }
        return ret += "]";
    }
    //数组转字符串模拟实现
    public static void main7(String[] args) {
        int[] arr = new int[]{1,2,3,4,5,6,7};
        String ret = MYArraystoString(arr);
        System.out.println(ret);
    }
    //作为参数使用
    public static void fun1(int[] temp){
        temp = new int[]{10,20,30};
        temp[0] = 100;
    }
    public static void fun2(int[] temp2){
        temp2[0] = 100;
    }
    public static void main6(String[] args) {
        int[] arr1 = new int[]{1,2,3,4};
        fun1(arr1);
        int[] arr2 = new int[]{1,2,3,4};
        fun2(arr2);
        System.out.println(Arrays.toString(arr1));
        System.out.println(Arrays.toString(arr2));

    }
    public static void main5(String[] args) {
        int num = 0;
        fun(num);
        System.out.println("num = "+num);
    }
    public static void fun(int x){
        x = 10;
        System.out.println("x = "+x);
    }
    //数组应用场景
    public static void main4(String[] args) {
        int[] a = new int[]{1,2,3,4};
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i]+" ");
        }
    }
    //数组的NULL
    public static void main3(String[] args) {
        int[] arr = null;
        //System.out.println(arr.length);
        //System.out.println(arr[0]);
    }
    //数组的引用
    public static void main2(String[] args) {
        int[] aray1 = new int[3];
        aray1[0] = 20;
        aray1[1] = 30;
        aray1[2] = 40;

        int[] aray2 = new int[]{1,2,3,4};
        aray2[1] = 100;
        aray2[2] = 200;
        //System.out.println(Arrays.toString(aray2));
        aray1 = aray2;
        aray1[0] = 100;
        aray2[0] = 10;
        System.out.println(Arrays.toString(aray1));
        System.out.println(aray1.length);
    }
    //数组的创建，输出；
    //创建
    public static void main1(String[] args) {
        int[] a = {1,2,3};
        a[0] = 100;
        //1
        System.out.println("第一种");
        System.out.println(a[0]);
        System.out.println(a[1]);
        System.out.println(a[2]);
        //2
        System.out.println("第二种");
        for (int i = 0; i < a.length; i++) {
            System.out.println(a[i]);
        }
        //3
        System.out.println("第三种");
        for (int x:
             a) {
            System.out.println(x);
        }
        //4
        System.out.println("第四种");
        String sum = Arrays.toString(a);
        System.out.println(sum);
        int[] b = new int[]{1,2,3};
        int[] c;
        c = new int[10];
        int[] d;
        d= new int[]{1,2,3};
    }
}
